<?php
namespace Admin\Controller;
//短信模板

use Common\Controller\AdminbaseController;

class SmstemplateController extends AdminbaseController{

	protected $sms_model;

	public function _initialize() {
		parent::_initialize();
		$this->sms_model = D("Common/SmsTemplate");
	}

	// 模板列表
	public function index(){
		/**搜索条件**/
		$sms_code = I('request.sms_code');
		$sms_name = I('request.sms_name');
		if($sms_code){
			$where['sms_code'] = $sms_code;
		}

		if($sms_name){
			$where['sms_name'] = array('like',"%$sms_name%");;
		}

		$count=$this->sms_model->where($where)->count();
		$page = $this->page($count, 20);
        $list = $this->sms_model
            ->where($where)
            ->order("id DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
		$this->assign("page", $page->show('Admin'));
        $this->assign('list',$list);
		$this->display();
	}

	// 模板添加
	public function add(){
		$this->display();
	}

	// 模板添加提交
	public function add_post(){
		if(IS_POST){
            if ($this->sms_model->create() !== false) {
                $date = $_POST;
                /*当前管理员name*/
                $date['create_user'] = get_current_admin_name();
                $date['create_time'] = get_admin_time();
                $result = $this->sms_model->add($date);
                if ($result !== false) {
                    $this->success("添加成功！", U("Smstemplate/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->sms_model->getError());
            }

		}
	}

	// 模板编辑
	public function edit(){
	    $id = I('get.id',0,'intval');
		$sms = $this->sms_model->where(array('id' => $id))->find();
		$this->assign('sms',$sms);
		$this->display();
	}

	// 模板编辑提交
	public function edit_post(){
		if (IS_POST) {
            if ($this->sms_model->create() !== false) {
                $date = $_POST;
                /*当前管理员name*/
                $date['update_user'] = get_current_admin_name();
                $date['update_time'] = get_admin_time();
                $result = $this->sms_model->save($date);
                if ($result !== false) {
                    $this->success("编辑成功！", U("Smstemplate/index"));
                } else {
                    $this->error("编辑失败！");
                }
            } else {
                $this->error($this->sms_model->getError());
            }

		}
	}

	// 模板删除
	public function delete(){
	    $id = I('get.id',0,'intval');
		if ($this->sms_model->delete($id)!==false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}

}